import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { PlatformLocation } from '@angular/common';
import { UserService } from '@nx-chat/common/service';

@Component({
  selector: 'chat-list',
  templateUrl: './chat-list.component.html',
  styleUrls: ['./chat-list.component.scss']
})
export class ChatListComponent implements OnInit {

  list = [];

  constructor(
    private _router: Router,
    private _activateRoute: ActivatedRoute,
    private platform: PlatformLocation,
    private _userService: UserService,
  ) {}

  public isActive(uid: string): boolean {
    const url = this.platform.pathname.substr(1);
    return url.includes(uid);
  }

  public getChat(uid: string) {
    this._router.navigate([`message/${uid}`]);
  }

  /**删除聊天列表的一项 */
  public delete(id: string) {
    this.list = this.list.filter(user => user.uid !== id);
    if (this.isActive(id)) {
      this._router.navigate([`../`], { relativeTo: this._activateRoute });
    }
  }

  ngOnInit() {
    const uid = this._userService.uId;
    this._userService.getFriendsList(uid).subscribe(data => {
      this.list = data;
    })
  }
}
